package pab.ui.acc;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
/*
 * Created by JFormDesigner on Wed Jun 08 18:39:58 ICT 2011
 */


import pab.core.exception.AccountException;
import pab.core.exception.TransactionException;
import pab.core.service.AccountService;
import pab.core.service.AccountServiceImpl;
import pab.core.service.ExchangeRate;
import pab.core.service.FixedExchangeRate;
import pab.core.service.WebExchangeRate;
import pab.core.transaction.Currency;
import pab.ui.GeneralAuxiliary;
import pab.ui.MainApp;



/**
 * @author rukowen
 */
public class AccountAdd extends JDialog {
	/**
	 * 
	 */
	public boolean isAddOk = false;
	private static final long serialVersionUID = 1L;
	
	AccountService accountService;
	public AccountAdd(JFrame parent, AccountService accountService) {
		super(parent, true);
		this.accountService = accountService;
		initComponents();
		this.setLocationRelativeTo(parent);
		
		txtCurrBalance.setHorizontalAlignment(JTextField.RIGHT);
		txtCurrBalance.addKeyListener(new KeyAdapter()
		   {
		       public void keyTyped (KeyEvent e) 
		        { 
		            char c = e.getKeyChar() ;
		                
		            if (!   ((c==KeyEvent.VK_BACK_SPACE) || (c==KeyEvent.VK_DELETE) 
		                ||  (c== KeyEvent.VK_ENTER)      || (c == KeyEvent.VK_TAB) 
		                ||  (Character.isDigit(c)))) 
		            {
		               e.consume() ;
		           } 
		        } 
		    });
	}
	
	
	private void btnOkMouseClicked(MouseEvent e) {
		// TODO add your code here
		
	}

	private void btnCancelMouseClicked(MouseEvent e) {
		// TODO add your code here
		isAddOk = false;
		this.dispose();
	}

	private void btnOkActionPerformed(ActionEvent e) {
		// TODO add your code here
		String accName = txtName.getText();
		String accNo = txtNumber.getText();
		
		String currBalance = txtCurrBalance.getText();
		double balance = Double.parseDouble(currBalance.trim().equals("")?"0":currBalance) ;
		Currency cur = accountService.getExchangeService().getCurrency(cboCurrency.getSelectedItem().toString());
		try {
			accountService.openAccount(accNo, accName, cur, balance);
			accountService.save();
			isAddOk = true;
			this.dispose();
		} catch (AccountException e1) {
			JOptionPane.showMessageDialog(this, e1.getMessage());
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}		
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		label2 = new JLabel();
		label3 = new JLabel();
		label4 = new JLabel();
		label5 = new JLabel();
		txtName = new JTextField();
		txtNumber = new JTextField();
		cboCurrency = new JComboBox();
		txtCurrBalance = new JFormattedTextField();
		btnOk = new JButton();
		btnCancel = new JButton();

		//======== this ========
		setTitle("Add a new account");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		Container contentPane = getContentPane();
		contentPane.setLayout(null);

		//---- label2 ----
		label2.setText("Name");
		contentPane.add(label2);
		label2.setBounds(40, 40, label2.getPreferredSize().width, 15);

		//---- label3 ----
		label3.setText("Number");
		contentPane.add(label3);
		label3.setBounds(new Rectangle(new Point(40, 70), label3.getPreferredSize()));

		//---- label4 ----
		label4.setText("Current Balance");
		contentPane.add(label4);
		label4.setBounds(new Rectangle(new Point(40, 100), label4.getPreferredSize()));

		//---- label5 ----
		label5.setText("Currency");
		contentPane.add(label5);
		label5.setBounds(new Rectangle(new Point(40, 135), label5.getPreferredSize()));
		contentPane.add(txtName);
		txtName.setBounds(140, 35, 200, txtName.getPreferredSize().height);
		contentPane.add(txtNumber);
		txtNumber.setBounds(140, 65, 200, 28);
		contentPane.add(cboCurrency);
		cboCurrency.setBounds(140, 125, 120, cboCurrency.getPreferredSize().height);
		contentPane.add(txtCurrBalance);
		txtCurrBalance.setBounds(140, 95, 200, txtCurrBalance.getPreferredSize().height);

		//---- btnOk ----
		btnOk.setText("Ok");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnOkActionPerformed(e);
			}
		});
		contentPane.add(btnOk);
		btnOk.setBounds(140, 160, 75, btnOk.getPreferredSize().height);

		//---- btnCancel ----
		btnCancel.setText("Cancel");
		btnCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				btnCancelMouseClicked(e);
			}
		});
		contentPane.add(btnCancel);
		btnCancel.setBounds(new Rectangle(new Point(270, 160), btnCancel.getPreferredSize()));

		{ // compute preferred size
			Dimension preferredSize = new Dimension();
			for(int i = 0; i < contentPane.getComponentCount(); i++) {
				Rectangle bounds = contentPane.getComponent(i).getBounds();
				preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
				preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
			}
			Insets insets = contentPane.getInsets();
			preferredSize.width += insets.right;
			preferredSize.height += insets.bottom;
			contentPane.setMinimumSize(preferredSize);
			contentPane.setPreferredSize(preferredSize);
		}
		setSize(390, 240);
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
		
		GeneralAuxiliary.loadCurrency(cboCurrency);
		this.setVisible(true);
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JLabel label2;
	private JLabel label3;
	private JLabel label4;
	private JLabel label5;
	private JTextField txtName;
	private JTextField txtNumber;
	private JComboBox cboCurrency;
	private JFormattedTextField txtCurrBalance;
	private JButton btnOk;
	private JButton btnCancel;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
